public class Piece extends Board {
	public static final Piece unitPiece = new Piece(new boolean[][] {
			{ true, false, false, false, },
			{ false, false, false, false, },
			{ false, false, false, false, },
			{ false, false, false, false, },
	});
	private final int[][] tCoords;
	public final int size;
	public final int nRows;
	public final int nColumns;
	public int rotationNeeded;
	public int xSelect;
	public int ySelect;
	
	public Piece(boolean[][] p) {
		this(0, p);
	}
	
	private Piece(int rotationN, boolean[][] p) {
		rotationNeeded = rotationN;
		size = new Board(p).getSize();
		if (size == 0) {
			tCoords = null;
			nRows = 0;
			nColumns = 0;
			return;
		}
		
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (p[i][j]) {
					xSelect = j;
					ySelect = i;
				}
			}
		}
		
		int minX = 0;
		int minY = 0;
		int maxX = 3;
		int maxY = 3;
		for (int doThreeTimes = 0; doThreeTimes < 3; doThreeTimes++) {
			minY += p[minY][0] || p[minY][1] || p[minY][2] || p[minY][3] ? 0 : 1;
			maxY -= p[maxY][0] || p[maxY][1] || p[maxY][2] || p[maxY][3] ? 0 : 1;
			minX += p[0][minX] || p[1][minX] || p[2][minX] || p[3][minX] ? 0 : 1;
			maxX -= p[0][maxX] || p[1][maxX] || p[2][maxX] || p[3][maxX] ? 0 : 1;
		}
		maxX++;
		maxY++;
		boolean[][] newP = new boolean[maxY - minY][maxX - minX];
		for (int i = minX; i < maxX; i++) {
			for (int j = minY; j < maxY; j++) {
				newP[j - minY][i - minX] = p[j][i];
			}
		}
		content = newP;
		nRows = newP.length;
		nColumns = newP[0].length;
		tCoords = initCoords();
	}
	
	public int getSize() {
		return size;
	}

	private int[][] initCoords() {
		int[][] res = new int[size][];
		int index = 0;
		for (int y = 0; y < nRows; y++) {
			for (int x = 0; x < nColumns; x++) {
				if (content[y][x]) {
					res[index] = new int[] { y, x };
					index++;
				}
			}
		}
		return res;
	}
	
	public Piece rotation3PiOver2() {
		//	· x x 		· · · · 
		//	· x ·  -->	x x x x
		//	· x ·  -->	· · · x 
		//	· x · 
		boolean[][] newContent = new boolean[4][4];
		for (int i = 0; i < nRows; i++) {
			for (int j = 0; j < nColumns; j++) {
				if (content[i][j]) {
					newContent[j][nRows - 1 - i] = true;
				}
			}
		}
		return new Piece(rotationNeeded + 1, newContent);
	}
	
	public int[][] getTCoords() {
		return tCoords; //.clone()
	}
	
	public static void main(String[] args) {
		System.out.println(unitPiece);
		System.out.println(unitPiece.rotation3PiOver2());
	}
}
